﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows.Input;
using QLTT.Models;
using QLTT.Properties;
using QLTT.Commands;
using System.Text.RegularExpressions;

namespace QLTT.ViewModels
{
    class ClassTimeViewModel:WorkspaceViewModel, System.ComponentModel.IDataErrorInfo
    {
        private ClassTime _classTime;
        private icanDataContext _dataContext;
        private DelegateCommand _saveCommand;
        private DelegateCommand _editCommand;
        private DelegateCommand _cancelCommand;
                
        private bool _isSelected;
        public ClassTimeViewModel(icanDataContext dataContext)
        {
            _dataContext = dataContext;
            _classTime = new ClassTime();
            base.DisplayName = Properties.Resources.Base_DisplayName_ClassTime_New;
        }
        public ClassTimeViewModel(icanDataContext dataContext, ClassTime classTime)
        {
            _classTime = classTime;
            _dataContext = dataContext;
            base.DisplayName = Properties.Resources.Base_DisplayName_ClassTime_Edit;
        }
       
        public string GetHashCode()
        {
            return string.Format("{0}-{1}", _classTime.StartTime, _classTime.EndTime);
        }
        public ICommand SaveCommand
        {
            get
            {
                if (_saveCommand == null)
                {
                    _saveCommand = new DelegateCommand(Save);
                }
                return _saveCommand;
            }
        }
        public ICommand EditCommand
        {
            get
            {
                if (_editCommand == null)
                {
                    _editCommand = new DelegateCommand(Edit);
                }
                return _editCommand;
            }
        }
        public ICommand CancelCommand
        {
            get
            {
                if (_cancelCommand == null)
                {
                    _cancelCommand = new DelegateCommand(Cancel);
                }
                return _cancelCommand;
            }
        }

        private void Save()
        {
            if (_dataContext == null || _classTime == null)
                return; 
            string[] properties = { "Name", "StartTime", "EndTime" };
            if (!ValidateData(properties))
                return;

            try
            {
                bool isInsert = _classTime.ID == 0;
                if (isInsert)
                {
                    _dataContext.ClassTimes.InsertOnSubmit(_classTime);
                }
                _dataContext.SubmitChanges();
                if(isInsert)
                    Mediator.NotifyColleagues<ClassTime>(Messages.ListClassTimeUpdated, _classTime);
                Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
            }
            catch (Exception ex)
            {
                this.ErrorMsg = Properties.Resources.ErrorMessage_General;
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
            
            
        }
       
        private void Edit()
        {
            if (_dataContext == null || _classTime == null)
                return;
            
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceAdd, this);
        }
        private void Cancel()
        {
            if(_classTime.ID > 0)
                _dataContext.Refresh(System.Data.Linq.RefreshMode.OverwriteCurrentValues, _classTime);
            Mediator.NotifyColleagues<WorkspaceViewModel>(Messages.WorkspaceRemove, this);
        }
        public void Delete()
        {
            if (_dataContext == null || _classTime == null)
                return;
            try
            {
                _dataContext.ClassTimes.DeleteOnSubmit(_classTime);
                _dataContext.SubmitChanges();
            }
            catch (Exception ex)
            {
                System.Diagnostics.EventLog.WriteEntry("Application", ex.ToString(), System.Diagnostics.EventLogEntryType.Error);
            }
        }
        public long ID
        {
            get { return _classTime.ID; }
        }
        public string Name
        {
            get { return _classTime.Name; }
            set
            {
                if (_classTime.Name == value) return;
                _classTime.Name = value;
                base.OnPropertyChanged("Name");
            }
        }
        public string StartTime
        {
            get { return _classTime.StartTime; }
            set
            {
                if (_classTime.StartTime == value) return;
                _classTime.StartTime = value;
                base.OnPropertyChanged("StartTime");
            }
        }

        public string EndTime
        {
            get { return _classTime.EndTime; }
            set
            {
                if (_classTime.EndTime == value) return;
                _classTime.EndTime = value;
                base.OnPropertyChanged("EndTime");
            }
        }
        
        public string FullName
        {
            get
            {
                if(!string.IsNullOrEmpty(_classTime.StartTime) && !string.IsNullOrEmpty(_classTime.EndTime))
                    return string.Format("{0} {1} - {2}", _classTime.Name, _classTime.StartTime, _classTime.EndTime);
                return Properties.Resources.Combo_AllClass_ClassTime_Default;
            }
            
        }
        
        public bool IsSelected
        {
            get { return _isSelected; }
            set
            {
                if (value == _isSelected)
                    return;

                _isSelected = value;

                base.OnPropertyChanged("IsSelected");
            }
        }
        #region IDataErrorInfo Members

        public string Error
        {
            get { return null; }
        }

        public string this[string property]
        {
            get
            {
                string msg = null;
                Regex rxValidate = new Regex(@"\d{2}:\d{2}", RegexOptions.IgnoreCase);
                switch (property)
                {
                    case "Name":
                        if (string.IsNullOrEmpty(this.Name))
                            msg = Resources.ValidationClassTimeName_Error;
                        break;
                    case "StartTime":
                        if (string.IsNullOrEmpty(this.StartTime) || this.StartTime.Length != 5 || !rxValidate.IsMatch(this.StartTime))
                            msg = Resources.ValidationClassTimeStartTime_Error;
                        break;
                    case "EndTime":
                        if (string.IsNullOrEmpty(this.EndTime) || this.EndTime.Length != 5 || !rxValidate.IsMatch(this.EndTime))
                            msg = Resources.ValidationClassTimeEndTime_Error;
                        break;
                    default:
                        throw new ArgumentException(
                            Resources.ValidationDefaultException_Error + property);
                }
                return msg;
            }
        }

        #endregion // IDataErrorInfo Members

    }
}

